# TOPNAME = top
TOPNAME = ysyxSoCFull

DEF_NVBOARD = yes

INC_PATH = $(NPC_HOME)/vsrc/include $(abspath ../ysyxSoC/perip/uart16550/rtl) $(abspath ../ysyxSoC/perip/spi/rtl)
IMG ?=

LDFLAGS = -lLLVM-11 -lreadline -ldl -pie
VERILATOR = verilator
# flags for vcd
VERILATOR_VCDFLAGS = --cc --exe --build -j 0 --trace --autoflush
# VERILATOR_VCDFLAGS = --cc --build -j 0 --trace

# nvboard {
ifdef DEF_NVBOARD
VERILATOR_CFLAGS += -MMD --build -cc  \
				-O3 --x-assign fast --x-initial fast --noassert
NXDC_FILES = $(NPC_HOME)/constr/top.nxdc
SRC_AUTO_BIND = $(abspath $(VBUILD_DIR)/auto_bind.cpp)
endif
# } nvboard end

VERILATOR_FLGAS = --timescale "1ns/1ns" --no-timing

## differential test
include ./vcsrc/npcSim/include/config/auto.conf

DIFF_REF_SO = $(NPCSIM)/tools/spike-diff/build/riscv32-spike-so
ifdef CONFIG_DIFFTEST_REF_NEMU
DIFF_REF_SO = $(NEMU_HOME)/build/riscv32-nemu-interpreter-so
endif

# directories or files for nvboard
# BUILD_DIR = ./build
# OBJ_DIR = $(BUILD_DIR)/obj_dir
# BIN = $(BUILD_DIR)/$(TOPNAME)

# directories or files for vcd
VBUILD_DIR = ./vbuild
VOBJ_DIR = $(VBUILD_DIR)/obj_dir
VBIN = $(VBUILD_DIR)/$(TOPNAME)
VBINCHISEL = $(VBUILD_DIR)/$(TOPNAME)-chisel
NPCAR = $(NPC_HOME)/vcsrc/npcSim/build/npcar.a
# NPCAR =
# NPC_EXE = $(VOBJ_DIR)/npc.exe

all: $(VBIN) # $(BIN)

# $(shell mkdir -p $(BUILD_DIR))
$(shell mkdir -p $(VBUILD_DIR))

# constraint file
SRC_AUR_BIND = $(abspath $(VBUILD_DIR)/auto_pin_bind.cpp)
$(SRC_AUR_BIND): $(NXDC_FILES)
	python3 $(NVBOARD_HOME)/scripts/auto_pin_bind.py $^ $@

# project source
VSRCS_CHISEL = $(shell find $(abspath $(NPC_CHISEL_HOME)/build) $(abspath ../ysyxSoC/perip) $(abspath ../ysyxSoC/build/) -name "*.sv" -o -name "*.v")
VSRCS = $(shell find $(abspath ./vsrc) $(abspath ../ysyxSoC/perip) $(abspath ../ysyxSoC/build/) -name "*.sv" -o -name "*.v")
CSRCS = $(shell find $(abspath ./csrc) -name "*.cpp")
CSRCS += $(SRC_AUR_BIND)
# VCSRCS = $(shell find $(abspath ./vcsrc) -name "*.cpp")
VCSRCS = $(shell find $(abspath ./vcsrc) -name "SoCSim.cpp")
VCSRCS += $(SRC_AUR_BIND)
NPCSIM = $(NPC_HOME)/vcsrc/npcSim
NPCSIMSRCS = $(shell find $(abspath $(NPCSIM)/src) -name "*.c")

# rules for NVBoard
ifdef DEF_NVBOARD
include $(NVBOARD_HOME)/scripts/nvboard.mk
endif

# rules for verilator
INCFLAGS = $(addprefix -I, $(INC_PATH))
CXXFLAGS += $(INCFLAGS) -DTOP_NAME="\"V$(TOPNAME)\""

# $(BIN): $(VSRCS) $(CSRCS) $(NVBOARD_ARCHIVE)
#   @rm -rf $(OBJ_DIR)
#   $(VERILATOR) $(VERILATOR_CFLAGS) \
#     --top-module $(TOPNAME) $^ \
#     $(addprefix -CFLAGS , $(CXXFLAGS)) $(addprefix -LDFLAGS , $(LDFLAGS)) \
#     --Mdir $(OBJ_DIR) --exe -o $(abspath $(BIN))

$(VBIN): sim

$(VBINCHISEL): simChisel

sim: $(VSRCS) $(VCSRCS) $(NPCAR)
	$(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!
	echo $(VSRCS)
	@rm -rf $(VOBJ_DIR)
	$(VERILATOR) $(VERILATOR_VCDFLAGS) $(VERILATOR_FLGAS)\
		--top-module $(TOPNAME) $^ \
		$(INCFLAGS)\
		$(addprefix -LDFLAGS , $(LDFLAGS))\
		--Mdir $(VOBJ_DIR) -o $(abspath $(VBIN))

simChisel: $(VSRCS_CHISEL) $(VCSRCS) $(NPCAR) $(NVBOARD_ARCHIVE)
	$(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!
	@rm -rf $(VOBJ_DIR)
	$(VERILATOR) $(VERILATOR_VCDFLAGS) $(VERILATOR_FLGAS)\
		--top-module $(TOPNAME) $^ \
		$(INCFLAGS)\
		$(addprefix -CFLAGS , $(CXXFLAGS)) $(addprefix -LDFLAGS , $(LDFLAGS))\
		--Mdir $(VOBJ_DIR) -o $(abspath $(VBINCHISEL))

# sim: $(VSRCS) $(VCSRCS)
#   $(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!
#   @rm -rf $(VOBJ_DIR)
#   $(VERILATOR) $(VERILATOR_VCDFLAGS) \
#     --top-module $(TOPNAME) $^ \
#     $(INCFLAGS)\
#     --Mdir $(VOBJ_DIR)
#   @g++ -c -o $(VOBJ_DIR)/simtool.o $(NPC_HOME)/vcsrc/simtool.cpp

$(NPCAR):
	@make -C $(NPC_HOME)/vcsrc/npcSim tarc

run: $(VBIN)
	@$^ -d $(DIFF_REF_SO) $(ARGS) $(IMG)

runChisel: $(VBINCHISEL)
	@$^ -d $(DIFF_REF_SO) $(ARGS) $(IMG)

gdb: $(VBIN)
	gdb -s $(VBIN) --args $(VBIN) -d $(NPCSIM)/tools/spike-diff/build/riscv32-spike-so $(ARGS) $(IMG)

menuconfig:
	@make -C $(NPC_HOME)/vcsrc/npcSim menuconfig

# vcd: $(VBIN)
#   @$^ $(IMG)

# run: $(BIN)
#   @$^


clean:
	rm -rf $(BUILD_DIR)
	rm -rf $(VBUILD_DIR)
	@make -C $(NPC_HOME)/vcsrc/npcSim clean

.PHONY: all sim vcd run clean

include ../Makefile

